﻿<template>
	<div class="a_SleepRecord-container">
		<el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
			<el-form :model="queryParams" ref="queryForm" :inline="true">
				<el-form-item label="持续时间">
					<el-input v-model="queryParams.duration" clearable placeholder="请输入持续时间" />
				</el-form-item>

				<el-form-item label="年份">
					<el-date-picker v-model="queryParams.years" type="year" />
				</el-form-item>

				<el-form-item label="月份">
					<el-date-picker v-model="queryParams.months" type="month" />
				</el-form-item>

				<el-form-item label="状态">
					<el-select clearable v-model="queryParams.state" placeholder="请选择状态">
						<el-option v-for="(item, index) in getEnumStateData" :key="index" :value="item.value" :label="item.describe"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery">查询</el-button>
						<el-button icon="ele-Refresh" @click="() => (queryParams = {})">重置</el-button>
					</el-button-group>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="ele-Plus" @click="openAddSleepRecord"> 新增</el-button>
				</el-form-item>
			</el-form>
		</el-card>

		<el-card class="full-table" shadow="hover" style="margin-top: 8px">
			<el-table :data="tableData" style="width: 100%" v-loading="loading" tooltip-effect="light" row-key="id" border="">
				<el-table-column type="index" label="序号" width="55" align="center" />

				<el-table-column prop="nowTime" align="center" label="日期" width="240" show-overflow-tooltip="">
					<template #default="scope"> {{ formatDate(scope.row.nowTime, 'YYYY-mm-dd') }}</template>
				</el-table-column>

				<el-table-column prop="startTime" align="center" label="开始时间" width="240" />

				<el-table-column prop="endTime" align="center" label="结束时间" width="240" show-overflow-tooltip="" />
				<el-table-column prop="duration" align="center" label="持续时间" width="240" show-overflow-tooltip="" />
				<el-table-column prop="state" align="center" label="状态" show-overflow-tooltip="">
					<template #default="scope">
						<el-tag :type="scope.row.state == 1 ? 'danger' : scope.row.state == 2 ? 'warning' : 'success'">
							{{ getEnumDesc(scope.row.state, getEnumStateData) }}
						</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="140" align="center" fixed="right" show-overflow-tooltip="">
					<template #default="scope">
						<el-button icon="ele-Edit" size="small" text="" type="primary" @click="openEditA_SleepRecord(scope.row)"> 编辑 </el-button>
						<el-button icon="ele-Delete" size="small" text="" type="primary" @click="delA_SleepRecord(scope.row)"> 删除 </el-button>
					</template>
				</el-table-column>
			</el-table>

			<el-pagination
				v-model:currentPage="tableParams.page"
				v-model:page-size="tableParams.pageSize"
				:total="tableParams.total"
				:page-sizes="[10, 20, 50, 100, 200]"
				small=""
				background=""
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
			/>
			<p>1、时间在0-6为缺少，6-8为一般或良好，良好需要在凌晨之前入睡</p>

			<editDialog ref="editDialogRef" :title="editA_SleepRecordTitle" @reloadTable="handleQuery" />
		</el-card>
	</div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';

import editDialog from '/src/views/develop/sleepRecord/component/editDialog.vue';
import { pageA_SleepRecord, deleteA_SleepRecord } from '/src/api/develop/sleepRecord';
import { getAPI } from '/src/utils/axios-utils';
import { SysEnumApi } from '/src/api-services/api';
import commonFunction from '/src/utils/commonFunction';

const getEnumStateData = ref<any>([]);

const { getEnumDesc } = commonFunction();

const editDialogRef = ref();

const loading = ref(false);
const tableData = ref<any>([]);
const queryParams = ref<any>({});

const tableParams = ref({
	page: 1,
	pageSize: 10,
	total: 0,
});

const editA_SleepRecordTitle = ref('');

// 查询操作
const handleQuery = async () => {
	queryParams.value.year = queryParams.value.years ? queryParams.value.years.getFullYear() : null;
	queryParams.value.month = queryParams.value.months ? queryParams.value.months.getMonth() + 1 : null;

	loading.value = true;
	var res = await pageA_SleepRecord(Object.assign(queryParams.value, tableParams.value));
	tableData.value = res.data.result?.items ?? [];

	tableParams.value.total = res.data.result?.total;
	loading.value = false;
	getEnumStateData.value = (await getAPI(SysEnumApi).apiSysEnumEnumDataListGet('SleepStateEnum')).data.result ?? [];
};

// 打开新增页面
const openAddSleepRecord = () => {
	editA_SleepRecordTitle.value = '添加';
	editDialogRef.value.openDialog({ nowTime: formatDate(new Date(), 'YYYY-mm-dd') });
};

// 打开编辑页面
const openEditA_SleepRecord = (row: any) => {
	editA_SleepRecordTitle.value = '编辑';
	editDialogRef.value.openDialog(row);
};

// 删除
const delA_SleepRecord = (row: any) => {
	ElMessageBox.confirm(`确定要删除吗?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			await deleteA_SleepRecord(row);
			handleQuery();
			ElMessage.success('删除成功');
		})
		.catch(() => {});
};

// 改变页面容量
const handleSizeChange = (val: number) => {
	tableParams.value.pageSize = val;
	handleQuery();
};

// 改变页码序号
const handleCurrentChange = (val: number) => {
	tableParams.value.page = val;
	handleQuery();
};

queryParams.value.years = new Date();
queryParams.value.months = new Date();

handleQuery();
</script>
